/*
 * Copyright (c) 2011 Anthony Vandikas
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "stdafx.h"
#include "SignOutWidget.h"

#include "AbstractInventory.h"
#include "InventoryModel.h"
#include "AbstractHistory.h"
#include "HistoryModel.h"
#include "AbstractVerifier.h"

SignOutWidget::SignOutWidget(AbstractInventory &inventory, AbstractHistory &history, AbstractVerifier &verifier)
	: inventory(&inventory), inventoryModel(new InventoryModel(inventory)),
	history(&history), historyModel(new HistoryModel(history, TimeColumn | StudentColumn | ItemColumn)),
	verifier(&verifier)
{
	ui.setupUi(this);

	ui.itemBox->setModel(inventoryModel);
	ui.entryView->setModel(historyModel);
}

SignOutWidget::~SignOutWidget()
{
	delete inventoryModel;
	delete historyModel;
}

void SignOutWidget::signOutItem()
{
	if (!verifyFields())
		return;

	auto identifier = ui.usernameBox->text().trimmed();
	auto password = ui.passwordBox->text().trimmed();
	auto item = inventoryModel->getItem(inventoryModel->index(ui.itemBox->currentIndex(), 0));

	bool success;
	Student student;
	std::tie(success, student) = verifier->verify(identifier, password, item);

	if (success)
	{
		history->record(student, item);

		// The fields need to be reset so that the next person doesn't sign something
		// out under another person's name.
		ui.usernameBox->clear();
		ui.passwordBox->clear();
	}
}

bool SignOutWidget::verifyFields() const
{
	// && is short circuiting, so if one of the tests fail, the user won't get messages for the others.
	return verifyField(ui.usernameBox, !ui.usernameBox->text().isEmpty(), "The student ID cannot be empty.") &&
		verifyField(ui.passwordBox, !ui.passwordBox->text().isEmpty(), "The password cannot be empty.") &&
		verifyField(ui.itemBox, ui.itemBox->currentIndex() >= 0, "Please choose an instrument.");
}

bool SignOutWidget::verifyField(QWidget *widget, bool isValid, const QString errorMessage) const
{
	if (isValid)
		return true;

	applicationInfo(errorMessage);
	widget->setFocus();

	return false;
}